import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';

import '../../resources/kq_theme_colors.dart';
import '../../resources/l10n/kq_intl.dart';
import '../../theme/kq_theme_manager.dart';
import '../button/kq_outline_button.dart';
import '../steps/kq_step_number_bar.dart';

/// 数字步进对话框
///
/// 数字减 + 输入框 + 数字加
///
/// @author 周卓
///
class KqStepNumberDialog extends AlertDialog {
  /// 标题
  final String? titleString;

  /// 步进值，默认1
  final num step;

  /// 最小值，默认0
  final num min;

  /// 最大值
  final num? max;

  /// 是否支持小数，默认false
  final bool decimal;

  /// 是否可编辑
  final bool editable;

  /// 输入框内容控制器
  final TextEditingController editController;

  /// 左边按钮文字，默认"取消"
  final String? leftBtnText;

  /// 左边按钮文字，默认"确定"
  final String? rightBtnText;

  /// 左边按钮回调
  final Function(String value)? onLeftBtnTap;

  /// 右边按钮回调
  final Function(String value)? onRightBtnTap;

  const KqStepNumberDialog({
    Key? key,
    this.titleString,
    this.decimal = false,
    this.editable = true,
    this.min = 0,
    this.step = 1,
    this.max,
    required this.editController,
    this.leftBtnText,
    this.rightBtnText,
    this.onLeftBtnTap,
    this.onRightBtnTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final List<Widget> children = <Widget>[];

    /// 上边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    if (titleString != null) {
      children.add(Container(
        padding: EdgeInsets.symmetric(horizontal: 16.r),
        alignment: Alignment.center,
        child: Text(
          titleString!,
          style: TextStyle(
            fontSize: 28.sp,
            color:
                KqThemeManager.instance.getConfig().dialogConfig.titleColor ??
                    KqThemeColors.textBlack85,
          ),
        ),
      ));
      children.add(SizedBox(
        width: 1.r,
        height: 24.r,
      ));
    }

    /// 输入框
    children.add(Container(
      margin: EdgeInsets.symmetric(horizontal: 40.r),
      alignment: Alignment.center,
      child: KqStepNumberBar(
        decimal: decimal,
        editable: editable,
        min: min,
        max: max,
        step: step,
        editController: editController,
      ),
    ));

    /// 下边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    /// 按钮
    children.add(Padding(
      padding: EdgeInsets.symmetric(horizontal: 40.r, vertical: 24.r),
      child: Row(
        children: [
          Expanded(
            child: KqOutlineButton.normal(
              title: leftBtnText ?? KqIntl.currentResource.cancel,
              verticalPadding: 22.r,
              onTap: (disabled) {
                if (onLeftBtnTap != null) {
                  onLeftBtnTap!(editController.text);
                }
              },
            ),
          ),
          SizedBox(
            width: 24.r,
          ),
          Expanded(
            child: KqOutlineButton(
              title: rightBtnText ?? KqIntl.currentResource.confirm,
              verticalPadding: 22.r,
              onTap: (disabled) {
                if (onRightBtnTap != null) {
                  onRightBtnTap!(editController.text);
                }
              },
            ),
          )
        ],
      ),
    ));

    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: children,
    );
    var padding = MediaQuery.of(context).viewInsets.bottom;
    return UnconstrainedBox(
        child: Padding(
      padding: EdgeInsets.only(bottom: padding),
      child: SizedBox(
          width: 880.r,
          child: Material(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(
                      KqThemeManager.instance.getConfig().dialogConfig.radius ??
                          8.r))),
              color: KqThemeColors.bgWhite,
              child: LimitedBox(
                maxHeight: 880.r,
                child: ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(
                      KqThemeManager.instance.getConfig().dialogConfig.radius ??
                          8.r)),
                  child: dialogChild,
                ),
              ))),
    ));
  }
}
